package management;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import utilities.Constants;
import model.Candidato;

public class ConfigurationParser {

	private static Integer id;
	private static ArrayList<Candidato> candidatos = new ArrayList<Candidato>(); 
	
	public static void parse(String file) {
		setId(file);
		fillCandidatesList(file);
	}

	private static void fillCandidatesList(String file) {
		Pattern candidatosPattern = ConfigurationParser.createPattern("candidatos");
		Pattern candidatoPattern = ConfigurationParser.createPattern("candidato");
		Pattern idPattern = ConfigurationParser.createPattern("id");
		Pattern votosPattern = ConfigurationParser.createPattern("cantVotos");
		Pattern nombrePattern = ConfigurationParser.createPattern("nombre");
		String candidatos = ConfigurationParser.getFirstGroup(file, candidatosPattern);
		Matcher candidatoMatcher = candidatoPattern.matcher(candidatos);
		while (candidatoMatcher.find()){
			String candidato = candidatoMatcher.group(1);
			Integer id = new Integer(getFirstGroup(candidato, idPattern));
			Integer cantVotos = new Integer (getFirstGroup(candidato, votosPattern)); 
			String nombre = getFirstGroup(candidato, nombrePattern);
			Candidato c = new Candidato(nombre, id);
			c.setCantVotos(cantVotos);
			ConfigurationParser.getCandidatos().add(c);
		}
	}

	private static void setId(String file) {
		Pattern idPattern = ConfigurationParser.createPattern("currentId");
		ConfigurationParser.id = new Integer(ConfigurationParser.getFirstGroup(file, idPattern));
	}
	
	public static Integer getId(){
		return ConfigurationParser.id;
	}
	
	private static Pattern createPattern(String field){
		return Pattern.compile("<" + field + ">(.*?)</" + field + ">", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
	}
	
	private static String getFirstGroup (String input, Pattern p){
		Matcher m = p.matcher(input);
		String result = null;
		if (m.find()){
			result = m.group(1);
		}
		return result;
	}

	public static ArrayList<Candidato> getCandidatos() {
		return candidatos;
	}
	

	public static void parseFields(String configuration) {
		try {
			String configFile = FileHelper.readFile(configuration, Charset.defaultCharset());
			Pattern configPattern = ConfigurationParser.createPattern("config");
			String bodyConfig = getFirstGroup(configFile, configPattern);
			Pattern salidaPattern = ConfigurationParser.createPattern("archivoSalida");
			String archivoSalida = getFirstGroup(bodyConfig, salidaPattern);
			Pattern imagePattern = ConfigurationParser.createPattern("carpetaImagen");
			String image = getFirstGroup(bodyConfig, imagePattern);
			Pattern configDirectoryPattern = ConfigurationParser.createPattern("carpetaConfig");
			String imgDirectory = getFirstGroup(bodyConfig, configDirectoryPattern);
			Constants.archivoVotacion = archivoSalida;
			Constants.defaultPath = image;
			Constants.newUserPath = imgDirectory + "addUser.png";
			Constants.backgroundPath = imgDirectory + "background.jpg";
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
